package Page.ResearchInfoPage;

import Dao.BookBorrowingRecordDao;
import Dao.CourseInfoDao;
import Dao.ResearchProjectDao;
import DataClass.ResearchProject;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class ResearchInfoController implements Initializable{
    @FXML
    public TextField InputKeyWord;
    @FXML
    public Button FindButton;
    @FXML
    public Button ClearButton;
    @FXML
    private Button BackButton;
    @FXML
    public Button RefreshButton;
    @FXML
    private TableView<ResearchProject> ResearchProjectTable;

    @FXML
    private TableColumn<ResearchProject,String> RPid;
    @FXML
    private TableColumn<ResearchProject,String> RPname;
    @FXML
    private TableColumn<ResearchProject,String> RPtype;
    @FXML
    private TableColumn<ResearchProject,String> RPstudent;
    @FXML
    private TableColumn<ResearchProject,String> RPtime;
    @FXML
    private TableColumn<ResearchProject,String> RPteacher;

    // viewModel
    private ResearchInfoViewModel viewModel;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        viewModel = new ResearchInfoViewModel();

        RPid.setCellValueFactory(new PropertyValueFactory<>("RPid"));
        RPname.setCellValueFactory(new PropertyValueFactory<>("RPname"));
        RPtype.setCellValueFactory(new PropertyValueFactory<>("RPtype"));
        RPstudent.setCellValueFactory(new PropertyValueFactory<>("RPstudent"));
        RPtime.setCellValueFactory(new PropertyValueFactory<>("RPtime"));
        RPteacher.setCellValueFactory(new PropertyValueFactory<>("RPteacher"));

        ResearchProjectTable.setItems(viewModel.getDataListProperty());

        InputKeyWord.textProperty().bindBidirectional(viewModel.getInputKeyWord());
        InputKeyWord.textProperty().addListener((observable, oldValue, newValue) -> {
            viewModel.updateInputKeyWord(newValue);
        });

        try {
            // 从数据库或其他来源获取数据
            viewModel.setDataList(ResearchProjectDao.getResearchProject());
        } catch (SQLException e) {
            viewModel.setDataList(new ArrayList<>());
            System.err.println("ResearchInfo 数据获取失败 in ResearchInfoController");
        }
    }

    public void backButtonClicked(javafx.event.ActionEvent actionEvent) {
        viewModel.backButtonClicked();
    }

    public void findButtonClicked(javafx.event.ActionEvent actionEvent) {
        viewModel.clickFindButton();
    }

    public void refreshButtonClicked(javafx.event.ActionEvent actionEvent) {
        viewModel.refresh();
        try {
            // 从数据库或其他来源获取数据
            viewModel.setDataList(ResearchProjectDao.getResearchProject());
        } catch (SQLException e) {
            viewModel.setDataList(new ArrayList<>());
            System.err.println("BookBorrowingRecord 数据获取失败" + e.getMessage());
        }
    }

    public void clearButtonClicked(ActionEvent actionEvent) {
        viewModel.clearFilter();
    }
}
